<?php
class Reservation extends MY_Controller{

	function __construct()
	{
		parent::__construct();
	}

	function index()
	{
		$this->load->library('form_validation');
		if($_POST){
			$this->form_validation->set_rules('name','nom & prénom', 'required');
			$this->form_validation->set_rules('telephone','téléphone', 'required');
			$this->form_validation->set_rules('email','email', 'required|valid_email');
			$this->form_validation->set_rules('date','date', 'required');
			$this->form_validation->set_rules('num');
			$this->form_validation->set_rules('time');
			$this->form_validation->set_error_delimiters('', '');
			$this->form_validation->set_message('rule', 'Error Message');
			$this->form_validation->set_message('valid_email', "has-error");
			$this->form_validation->set_message('required', "has-error");
			if($this->form_validation->run() == false){

			}else{
				$mailData = $_POST;
				$data['mailData'] = $mailData;
				$emailAdmin = $this->send_email_admin($mailData);
				if($emailAdmin){
					redirect('reservation/mailSucceed');
				}else{
					redirect('reservation/mailFailed');
				}

			}
		}
		$data['title'] = $this->title('Réservation');
		$this->load->view($this->theme.'/header',$data);
		$this->load->view($this->theme.'/reservation');
		$this->load->view($this->theme.'/footer');
	}

	function mailSucceed()
	{
		$data['title'] = $this->title('Réservation');
		$data['result'] = TRUE;
		$this->load->view($this->theme.'/header',$data);
		$this->load->view($this->theme.'/mailResult');
		$this->load->view($this->theme.'/footer');
	}

	function mailFailed()
	{
		$data['title'] = $this->title('Réservation');
		$data['result'] = false;
		$this->load->view($this->theme.'/header',$data);
		$this->load->view($this->theme.'/mailResult');
		$this->load->view($this->theme.'/footer');
	}



	private function send_email_admin($mailData)
	{

		$data['mailData'] = $mailData;
		$config['protocol'] = 'mail';
		$config['wordwrap'] = FALSE;
		$config['mailtype'] = 'html';
		$config['charset'] = 'utf-8';
		$config['crlf'] = "\r\n";
		$config['newline'] = "\r\n";
		$this->email->initialize($config);
		$this->email->from($mailData['email'], $mailData['email']);
		$this->email->to('info@betalife.be');
		$this->email->subject('Réservation | palaisbanquet.be');

		$email = $this->load->view($this->theme.'/emails/admin',$data,TRUE);
		$this->email->message($email);
		if($this->email->send()){
			return TRUE;
		}else{
			return FALSE;
		}
	}


}